package auction.chapter16.dao.hibernate;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import auction.chapter16.dao.ItemDAO;
import auction.model.Bid;
import auction.model.Item;

public class ItemDAOHibernate extends GenericHibernateDAO<Item, Long> implements
		ItemDAO {

	public Bid getMaxBid(Long iid) {
		Item i=this.findById(iid, false);
		Criteria c=getSession().createCriteria(Bid.class)
					.add(Restrictions.eq("item", i))
					.addOrder(Order.desc("price"))
					.setMaxResults(1);
		
		return (Bid)c.uniqueResult();
	}

	public Bid getMinBid(Long iid) {
		Item i=this.findById(iid, false);
		Criteria c=getSession().createCriteria(Bid.class)
					.add(Restrictions.eq("item", i))
					.addOrder(Order.asc("price"))
					.setMaxResults(1);
		
		return (Bid)c.uniqueResult();
	}

}
